import wepy from 'wepy';
// 手机号码格式验证  ！！！！不正确弃用！！！！ 2019/10/17 志彬
function vailPhone(number) {
  let flag = true
  let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/
  if (number.length !== 11 || !myreg.test(number)) {
    flag = false
  }
  return flag
}
//排除数组重复
function removeDuplicatedItem(arr) { 
  for(var i = 0; i < arr.length-1; i++){
      for(var j = i+1; j < arr.length; j++){
          if(arr[i]==arr[j]){
             arr.splice(j,1);
             j--;
          }
      }
  }
  return arr; 
}

//排除对话消息重复
function removeDuplicatedItem_msg(arr) { 
  for(var i = 0; i < arr.length-1; i++){
      for(var j = i+1; j < arr.length; j++){
          if(arr[i].msg_time==arr[j].msg_time){
             arr.splice(j,1);
             j--;
          }
      }
  }
  return arr;
}
//排除数组中的重复对象
function deteleObject(obj) {
  var uniques = [];
  var stringify = {};
  for (var i = 0; i < obj.length; i++) {
      var keys = Object.keys(obj[i]);
      keys.sort(function(a, b) {
          return (Number(a) - Number(b));
      });
      var str = '';
      for (var j = 0; j < keys.length; j++) {
          str += JSON.stringify(keys[j]);
          str += JSON.stringify(obj[i][keys[j]]);
      }
      if (!stringify.hasOwnProperty(str)) {
          uniques.push(obj[i]);
          stringify[str] = true;
      }
  }
  uniques = uniques;
  return uniques;
  }

//是否显示more
function isMoreShow(obj)
{
  let state;
  if(obj != '')
  {
    state = true;
  }
  else
  {
    state = false;
  }
  return state;
}

//验证身份证码号
function isCardNo(card)
{
  let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if(!reg.test(card))
  {
    wepy.showToast({
      title: '身份证号码格式错误，请重新输入！', 
      icon: 'none'
    }); 
    return false;
  }
  else
  {
    return true;
  }
}

//验证电子邮箱
function checkEmail(str){
  let reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
  if(!reg.test(str))
  {
    wepy.showToast({
      title: '电子邮箱格式错误，请重新输入！', 
      icon: 'none'
    }); 
    return false;
  }
  else
  {
    return true;
  }
}

//返回上一页
function returnPage()
{
  var pages = getCurrentPages();
  let prevPage = pages[pages.length - 2];
  wepy.navigateBack({
    delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
  });
}


module.exports = {
    vailPhone,
    removeDuplicatedItem,
    removeDuplicatedItem_msg,
    deteleObject,
    isMoreShow,
    isCardNo,
    checkEmail,
    returnPage
}